package com.xszx.business.controller.admin;

import com.xszx.server.dto.AjaxJson;
import com.xszx.server.dto.Page;
import com.xszx.server.entity.Category;
import com.xszx.server.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/category")
@Api(tags = "课程分类管理")
public class CategoryController {
    @Resource
    private CategoryService categoryService;

    @ApiOperation(value = "查询全部分类",notes = "将一级分类和二级分类全部查询")
    @PostMapping("/getAllCategorys")
    public AjaxJson getAllCategorys(){
        return AjaxJson.success().put("categorys",categoryService.getAllCategorys());
    }

    @ApiOperation(value = "查询单个分类",notes = "根据分类的ID查询单条分类数据")
    @GetMapping("/getCategory")
    public AjaxJson getCategory(@ApiParam(name = "id",value = "id是分类的主键必须传参",required = true) String id){
        return AjaxJson.success().put("categorys",categoryService.getCategory(id));
    }

    @ApiOperation(value = "保存或者更新分类",notes = "可以进行新增和更新操作")
    @PostMapping("/save")
    public AjaxJson save(@RequestBody Category category){
        return AjaxJson.success(categoryService.saveOrUpdate(category));
    }

    @ApiOperation(value = "删除分类",notes = "根据主键删除数据,进行的是物理删除")
    @GetMapping("/deleteCategory")
    public AjaxJson deleteCategory(String id){
        int result = categoryService.del(id);
        if(result>0){
            return AjaxJson.success("删除成功");
        }else{
            return AjaxJson.error("删除失败");
        }
    }
}
